<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * @author  Tran Van Thanh
 * @email   thanhtran@vietnambiz.com
 * @date    01.09.2011
 */
 
class Questions_model extends MY_Model {
    
    function __construct()
    {
        parent::__construct();
        $this->_table = 'questions';
    }
    
    function get_questions($page = 1)
    {
    	$limit = $this->config->item('questions', 'limit');
    	$this->db->order_by('date_create DESC');
    	return $this->limit($limit, ($page - 1)*$limit)->get_all();
    }        
    
    function get_questions_info($id_question = null)
    {
    	$this->db->select("a.*,b.fullname")
                 ->from('questions as a')
                 ->join("customers as b","a.id_customer = b.id")
                 ->where('a.id',$id_question);
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();
        return $result;	
    }

    function delete_question($question_id)
    {     
    	if(is_numeric($question_id)) {
        	$a[] = $question_id;
        	$question_id = $a;
        }
    	$this->db->where_in('id', $question_id)->delete('questions');    	
    	$this->delete_many($question_id);    	
    }
    
    function get_customer_info($id_customer = null)
    {    	
    	$this->db->select()
                 ->from('customers')
                 ->where('id',$id_customer);
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();
        return $result;
    }
    
    function get_answer_info_by_id_questions($question_id = null)
    {
    	$this->db->select()
                 ->from('answers')
                 ->where('id_question',$question_id);
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();
        return $result;
    }
    
	function get_id_products_with_questions($question_id = null)    
    {
    	$this->db->select('id_product')
                 ->from('questions_products')
                 ->where('id_question',$question_id);
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();
        return $result;
    }
}